/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2013 www.pretty-tools.com
 */

var gLogger = null;
try
{
    gLogger = yrprxFactory.createLogger("wsshImportRulesDlg");
}
catch(ex)
{
    dump("Cannot create logger! Exception: " + ex + "\n");
}

function hideProgress()
{
    try
    {
        var progressmeter = document.getElementById("progressmeter");
        if (progressmeter) progressmeter.style.display = "none";
    }
    catch(ex)
    {
        if (gLogger.enabled) gLogger.error("hideProgress: ", ex);
    }
}

function enableImport(enable)
{
    try
    {
        var accept = getAcceptButton();
        if (!accept) throw "Cannot get accept button!";

        if (enable)
            accept.removeAttribute("disabled");
        else
            accept.setAttribute("disabled", true);
    }
    catch(ex)
    {
        if (gLogger.enabled) gLogger.error("enableImport: ", ex);
    }
}

function updateButtons()
{
    try
    {
        if (gUserProfile.length) enableImport(true);

        if (wsshRulesImportManager.containsJavaScript(gUserProfile))
        {
            var enableJavascript = document.getElementById("enable_javascript");
            enableJavascript.removeAttribute("disabled");
        }
    }
    catch(ex)
    {
        if (gLogger.enabled) gLogger.error("updateButtons: ", ex);
    }
}

function onLoad()
{
    try
    {
        // init global data
        initRuleTree();
        //disable button
        enableImport(false);
        // load rules
        var url = window.arguments[0];
        if (gLogger.enabled) gLogger.debug("importFromURL: importing rules from " + url);
        wsshRulesImportManager.loadProfileFromURLAsync(url, function (items, lastModified) {
            hideProgress();

            if (!items || !items.length)
            {
                yrprxPrompt.alert(wsshResources["webslideshow.import.title"], wsshResources["webslideshow.cannot.import.rules"]);
                yrprxBrowser.closeWindow();
                return;
            }

            if (wsshRulesImportManager.containsJavaScript(items))
            {
                yrprxPrompt.alert(wsshResources["webslideshow.import.title"], wsshResources["webslideshow.import.rules.contains.js.you.should.check.box"]);
                wsshRulesImportManager.disableJavaScript(items);
            }

            gUserProfile = items;
            gSaveRules = true;
            updateUserRules();
        });
    }
    catch(ex)
    {
        if (gLogger.enabled) gLogger.error("onLoad: ", ex);
    }
}

function onOK()
{
    try
    {
        if (!gSaveRules) throw "Invalid state!";

        if (wsshRulesImportManager.containsJavaScript(gUserProfile))
        {
            var enableJavascript = document.getElementById("enable_javascript");
            if (enableJavascript.checked) // enable javascript
                wsshRulesImportManager.enableJavaScript(gUserProfile);
        }

        var currentProfile = wsshWebProfileSerializer.loadUserProfile();
        var merged = wsshWebProfileMergeHelper.merge(currentProfile, gUserProfile);
        wsshWebProfileSerializer.saveUserProfile(merged);

        // notify observers
        yrprxObserverServiceHelper.notifyObservers(null, WEBSLIDESHOW_UPDATE_RULES_EVENT, null);

        return true;
    }
    catch(ex)
    {
        if (gLogger.enabled) gLogger.error("onOK: ", ex);
    }
    return false;
}
